﻿(function($) {
    var methods = {
        fadeInTime: 3000,
        fadeOutTime: 1500,
        delayTime: 0,
        pendingCancelation: false,
        init: function(options) {
            return this.each(function() {
                var settings = { fadeInTime: 3000,
                    fadeOutTime: 1500,
                    delayTime: 0
                };
                if (options) {
                    $.extend(settings, options);
                }
                var index = 0;
                var top = $(this).css("top");
                $(this).find("div.slide").each(function() { $(this).css({ "display": "none", "position": "absolute", "top": $(this).css("top") }); });
                var totalSlide = $(this).find("div.slide").size();
                if (totalSlide > 0) {
                    index = 1;
                    methods.ShowNext({ control: this, top: top, index: 0, totalSlide: totalSlide, settings: settings, pendingCancelation: false });
                }

                return this;
            });
        },
        ShowNext: function(slides) {
            if (slides.index <= 0 || slides.index > slides.totalSlide) {
                slides.index = 1;
            }
            $(slides.control)
                        .find("div.slide:nth-child(" + slides.index + ")")
                        .css({ "z-index": slides.totalSlide })
                        .fadeTo(slides.settings.fadeInTime, 1.0)
                        .delay(slides.settings.delayTime)
            //.fadeTo(slides.settings.fadeOutTime, 0.4, function() { methods.SwitchSlide(slides, this); });
                        .fadeTo(slides.settings.fadeOutTime, 0.0, function() { methods.SwitchSlide(slides, this); });
        },
        SwitchSlide: function(slides, slide) {
            if (!$(slides.control).hasClass("slidshowstop")) {
                $(slide).css("z-index", slides.index);
                slides.index++;
                methods.ShowNext(slides);
                $(slide).fadeOut(1000);
            }
        },
        stop: function() {
            return $(this).each(function() {
                $(this).addClass("slidshowstop");
            });
        }
    };

    $.fn.SlideShow = function(method) {
        if (methods[method] && method == 'stop') {
            methods[method].apply(this, arguments);
        }
        else {
            methods.init.apply(this, arguments);
        }
        return this;
    };

})(jQuery);